package com.xinbochuang.machine.admin.controller.macroom;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinbochuang.machine.admin.domain.MacRoomResource;
import com.xinbochuang.machine.admin.domain.MacRoomResourceView;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.service.IMacRoomResourceService;
import com.xinbochuang.machine.admin.service.IMacRoomResourceViewService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;


/**
 * @author 杜文博
 * @date 2020-09-08
 */
@RestController
@RequestMapping("/mac-room-resource")
public class MacRoomResourceController extends BaseController {
    @Resource
    private TokenService tokenService;

    @Autowired
    private IMacRoomResourceViewService iMacRoomResourceViewService;

    @Autowired
    private IMacRoomResourceService iMacRoomResourceService;


    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @GetMapping("/selectMacRoomResourceData")
    public TableDataInfo selectMacRoomResourceData(MacRoomResource macRoomResource) {
        return iMacRoomResourceViewService.selectMacRoomResourceData(macRoomResource);
    }

    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo listResource(MacRoomResourceView macRoomResourceView) {
        IPage<MacRoomResourceView> reuslt = iMacRoomResourceViewService.listByPage(macRoomResourceView);
        return getDataTable(reuslt);
    }

    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @PostMapping("/addResource")
    public AjaxResult addResource(MacRoomResource macRoomResourceView) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        macRoomResourceView.setUuid(UUID.randomUUID().toString());
        macRoomResourceView.setCreateTime(DateUtil.now());
        macRoomResourceView.setCreateBy(user.getUserId() + "");
        macRoomResourceView.setDeleteFlag("0");
        return toAjax(iMacRoomResourceService.save(macRoomResourceView));
    }

    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @PostMapping("/updateResource")
    public AjaxResult updateResource(MacRoomResource macRoomResourceView) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        macRoomResourceView.setUpdateTime(DateUtil.now());
        macRoomResourceView.setUpdateBy(user.getUserId() + "");
        return toAjax(iMacRoomResourceService.updateById(macRoomResourceView));
    }

    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @PostMapping("/deleteResource")
    public AjaxResult deleteResource(MacRoomResource macRoomResourceView) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        macRoomResourceView.setUpdateTime(DateUtil.now());
        macRoomResourceView.setUpdateBy(user.getUserId() + "");
        macRoomResourceView.setDeleteFlag("9");
        return toAjax(iMacRoomResourceService.updateById(macRoomResourceView));
    }


    /**
     * 根据机房名称查询机房信息
     *
     * @return
     */
    @GetMapping("/selectMacRoomResourceDataByCharts")
    public AjaxResult selectMacRoomResourceDataByCharts(MacRoomResource macRoomResource) {
        return AjaxResult.data(iMacRoomResourceViewService.selectMacRoomResourceDataByCharts(macRoomResource));
    }

    /**
     * 机房资源统计
     */
    @GetMapping("/resourceCount")
    public AjaxResult resourceCount() {
        AjaxResult ar = AjaxResult.data(iMacRoomResourceViewService.resourceCount());
        ar.put("total", iMacRoomResourceViewService.resourceTotal());
        return ar;
    }
}

